package com.cx.bank.servlet;

import com.cx.bank.handler.MyInVocationHandler;
import com.cx.bank.manager.IManager;
import com.cx.bank.manager.ManagerImpl;
import com.cx.bank.pojo.User;
import com.cx.bank.vo.UserTypeEnum;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.sql.SQLException;
import java.util.List;

/**
 * 执行登录
 * @author jxau_scf
 * @date 2021/8/4 - 17:18
 */
@WebServlet(urlPatterns = "/doLogin")
public class DoLoginServlet extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        super.doGet(req, resp);
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        String username = req.getParameter("username");
        String password = req.getParameter("password");

        if ("".equals(username) && "".equals(password)){
            req.getRequestDispatcher("/WEB-INF/jsp/error.jsp").forward(req, resp);
        }

        //获取代理对象，方便使用日志
        IManager manager = new ManagerImpl();
        MyInVocationHandler handler = new MyInVocationHandler();
        handler.setUserName(username);
        handler.setTarget(manager);
        IManager proxy = (IManager)handler.getProxy();

        try {
            //调用登录方法，返回状态
            boolean loginStatus = proxy.login(username, password);
            User user = null;
            if (loginStatus){
                //登录成功
                user = proxy.findUserByName(username);
                //如果是管理员
                if (UserTypeEnum.ADMIN_USER.getCode().equals(user.getUserType())){
                    List<User> userList = proxy.findAllUser();
                    System.out.println(userList.toString());
                    req.setAttribute("userlist", userList);

//                    List<User> userlistAttribute = (List)req.getAttribute("userlist");
//                    System.out.println("userlistAttribute=================>"+userlistAttribute.toString());
                    //服务端跳转（转发）
                    req.getRequestDispatcher("/WEB-INF/jsp/user/admin/allUser.jsp").forward(req, resp);
                }
                //如果是普通用户
                if (UserTypeEnum.ORDINARY_USER.getCode().equals(user.getUserType())){
//                    req.setAttribute("balance", user.getBalance());
                    req.setAttribute("user", user);
                    //服务端跳转（转发）
                    req.getRequestDispatcher("/WEB-INF/jsp/user/individualAccount.jsp").forward(req, resp);
                }

            }else {
                //登录失败
                req.getRequestDispatcher("/WEB-INF/jsp/error.jsp").forward(req, resp);
            }

        } catch (SQLException e) {
            e.printStackTrace();
        }


    }
}
